package com.shy.config.util;

import cn.hutool.core.collection.CollectionUtil;
import com.shy.config.common.TokenConstants;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 关闭服务时清除redis中的令牌服务类
 */
@Component
@Slf4j
public class ShutDowListener implements ApplicationListener<ContextClosedEvent> {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        log.info("动力云客系统退出正在关闭服务........");
        //从redis中获取所有的token
        Set<String> keys = stringRedisTemplate.keys(TokenConstants.TOKEN_PREFIX + "*");
        //删除redis中所有用户的token
        if (CollectionUtil.isNotEmpty(keys)) {
            stringRedisTemplate.delete(keys);
        }

    }
}
